from sqlalchemy.orm import Session

from .database import engine
from . import models, baseModel, dependencies

models.Base.metadata.create_all(bind=engine)


# def get_user(db: Session, user_id: int):
#     return db.query(models.User).filter(models.User.id == user_id).first()
#
#
def get_user_by_account(db: Session, account: str):
    """
    从账号获取用户信息
    :param db:数据库会话
    :param account: 账号
    :return: 用户信息
    """
    db_user = db.query(models.User).filter(models.User.account == account).first()
    if not db_user:
        return db_user
    return baseModel.UserInDB(**db_user.__dict__)


#
# def get_users(db: Session, skip: int = 0, limit: int = 100):
#     return db.query(models.User).offset(skip).limit(limit).all()
#
#
def create_user(
        db: Session,
        user_create: baseModel.UserCreate,
        power: baseModel.Power = baseModel.Power.level
):
    """
    创建账号
    :param db: 数据库
    :param user_create: 创建账号的信息
    :return: 新用户信息
    :param power: 用户的权限等级 默认是一级
    """
    if len(user_create.password) < 6 or len(user_create.account) < 6:
        return False

    fake_hashed_password = dependencies.get_password_hash(user_create.password)
    db_user = models.User(
        account=user_create.account,
        name=user_create.name,
        power=power,
        hashed_password=fake_hashed_password
    )
    db.add(db_user)
    db.commit()
    db.refresh(db_user)
    return db_user
#
#
# def get_items(db: Session, skip: int = 0, limit: int = 100):
#     return db.query(models.Item).offset(skip).limit(limit).all()
#
#
# def get_item_by_owner(db: Session, owner_id):
#     return db.query(models.Item).filter(models.Item.owner_id == owner_id).all()
#
#
# def create_user_item(db: Session, item: schemas.ItemCreate, user_id: int):
#     db_item = models.Item(**item.model_dump(), owner_id=user_id)
#     db.add(db_item)
#     db.commit()
#     db.refresh(db_item)
#     return db_item
